package com.attendcollatesys.controller.excelcontroller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.attendcollatesys.entity.ATLeaveExtraDetail;
import com.attendcollatesys.entity.AtMonthTotalsPO;
import com.attendcollatesys.entity.excel.ExcelATDayDetails;
import com.attendcollatesys.excel.linstener.ExcelATDayDetailListener;
import com.attendcollatesys.excel.linstener.ExcelATLeaveExtraDetailListener;
import com.attendcollatesys.excel.linstener.ExcelATMonthTotalsListener;
import com.attendcollatesys.service.ATDayDetailsService;
import com.attendcollatesys.service.ATLeaveExtraDetailService;
import com.attendcollatesys.service.AtMonthTotalsService;
import com.attendcollatesys.service.ExcelService;
import com.attendcollatesys.web.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    private ATDayDetailsService atDayDetailsService;

    @Autowired
    private ATLeaveExtraDetailService atLeaveExtraDetailService;

    @Autowired
    private AtMonthTotalsService atMonthTotalsService;

    @Autowired
    private ExcelService excelService;

    /**
     * add by gzl
     * 2024年1月26日
     *
     * @param file 接收到的excel文件
     */
    @PostMapping("/uploadFile")
    public ResponseResult<?> handleFileUpload(@RequestParam("file") MultipartFile file) throws Exception {
//        //处理每日考勤明细sheet 请查阅ExcelATDayDetailLnstener
//        EasyExcel.read(file.getInputStream(), ExcelATDayDetails.class, new ExcelATDayDetailListener(atDayDetailsService))
//                .sheet(1)//读取第二个sheet页
//                .headRowNumber(4) //忽略前四行
//                .doRead();
//        //读多个或者全部sheet,一个sheet不能读取多次，多次读取需要重新读取文件
//        EasyExcel.read(file.getInputStream(), ATLeaveExtraDetail.class, new ExcelATLeaveExtraDetailListener(atLeaveExtraDetailService))
//                .sheet(2)//读取第三个sheet页
//                .headRowNumber(3) //忽略前三行
//                .doRead();
//        EasyExcel.read(file.getInputStream(), AtMonthTotalsPO.class, new ExcelATMonthTotalsListener(atMonthTotalsService))
//                .sheet(0)//读取第一个sheet页
//                .headRowNumber(4) //忽略前四行
//                .doRead();

        //modify by gzl  2024年2月2日
        //上面读取文件的方法会重复获取流
        ReadSheet build1 = EasyExcel.readSheet(0)
                .headRowNumber(4)  //这个设定是前四行为表头 在读取表格内容的时候不会读取前四行 但是监听器中的invokeHead方法是会读取前四行的 因为这里设定前四行为表头
                //不管如何设定 easyExcel是会读取所有行的 只不过读取表头 和内容所调用的方法不一样 详见对应监听器
                .head(AtMonthTotalsPO.class)
                .registerReadListener(new ExcelATMonthTotalsListener(atMonthTotalsService))
                .build();
        ReadSheet build2 = EasyExcel.readSheet(1)
                .headRowNumber(4)
                .head(ExcelATDayDetails.class)
                .registerReadListener(new ExcelATDayDetailListener(atDayDetailsService))
                .build();
        ReadSheet build3 = EasyExcel.readSheet(2)
                .headRowNumber(3)
                .head(ATLeaveExtraDetail.class)
                .registerReadListener(new ExcelATLeaveExtraDetailListener(atLeaveExtraDetailService)).
                build();

        EasyExcel.read(file.getInputStream()).build().read(build1, build2, build3);
        return ResponseResult.okResult();

    }

    @PostMapping("getResultExcel")
    public void getResultExcel(String[] date, String rtxNumber,
                               HttpServletResponse response) {
        excelService.getExcelResult(Arrays.stream(date).collect(Collectors.toList()), rtxNumber, response);
    }

    @GetMapping("checkExcelExist")
    public ResponseResult<?> checkExcelExist(String[] date, String rtxNumber){
        if(excelService.checkExcelExist(Arrays.stream(date).collect(Collectors.toList()), rtxNumber)){
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(400,"存在月份无数据 请核查");
        }

    }
}
